package org.wenhuayuan.library.log;

import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import org.wenhuayuan.library.util.HiDisplayUtil;

public class HiViewPrinterProvider {
    private static final String TAG_FLOATING_VIEW = "TAG_FLOATING_VIEW";// 悬浮按钮
    private static final String TAG_LOG_VIEW = "TAG_LOG_VIEW";// 悬浮窗


    private FrameLayout rootView;
    private View floatingView;
    private boolean isOpen;
    private FrameLayout logView;
    private RecyclerView recyclerView;

    public HiViewPrinterProvider(FrameLayout rootView, RecyclerView recyclerView) {
        this.rootView = rootView;
        this.recyclerView = recyclerView;
    }
    // 显示悬浮窗按钮
    public void showFloatingView() {
        // 如果界面中添加过了悬浮窗
        if(rootView.findViewWithTag(TAG_FLOATING_VIEW) != null) {
            return;
        }

        FrameLayout.LayoutParams params =
                new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.BOTTOM | Gravity.END;
        params.bottomMargin = HiDisplayUtil.dp2px(140, recyclerView.getResources());
        View floatView = generationFloatView();
        floatView.setTag(TAG_FLOATING_VIEW);
        floatView.setBackgroundColor(Color.DKGRAY);
        floatView.setAlpha(0.8f);
        rootView.addView(floatView, params);
    }

    // 创建悬浮窗按钮
    private View generationFloatView() {
        if(floatingView != null) {
            return floatingView;
        }
        TextView textView = new TextView(rootView.getContext());
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!isOpen) {
                    // 如果悬浮窗为打开，则显示
                    showLogView();
                }
            }
        });
        textView.setTextColor(Color.WHITE);
        textView.setText("查看日志");
        return this.floatingView = textView;
    }
    // 显示悬浮窗[日志窗口]
    private void showLogView() {
        if (rootView.findViewWithTag(TAG_LOG_VIEW) != null) {
            return;
        }
        FrameLayout.LayoutParams params =
                new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, HiDisplayUtil.dp2px(160, rootView.getResources()));
        params.gravity = Gravity.BOTTOM;
        View logView = generationLogView();
        logView.setTag(TAG_LOG_VIEW);
        rootView.addView(logView, params);
//        closeFloatingView();
        isOpen = true;
    }
    // 隐藏悬浮窗按钮
    private void closeFloatingView() {
        rootView.removeView(generationFloatView());
    }
    // 创建悬浮窗[日志窗口]
    private View generationLogView() {
        if (logView != null) {
            return logView;
        }
        FrameLayout logView = new FrameLayout(rootView.getContext());
        logView.setBackgroundColor(Color.DKGRAY);
        logView.addView(recyclerView);
        // 右上角设置一个关闭按钮
        FrameLayout.LayoutParams params =
                new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.TOP | Gravity.END;
        TextView closeView = new TextView(rootView.getContext());
        closeView.setTextColor(Color.WHITE);
        closeView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isOpen) {
                    // 如果悬浮窗为打开，则显示
                    closeLogView();
                }
            }
        });
        closeView.setText("关闭");
        logView.addView(closeView, params);
        return this.logView = logView;
    }
    // 关闭悬浮窗[日志窗口]
    private void closeLogView() {
        isOpen = false;
        rootView.removeView(generationLogView());
    }
}
